package com.teatrace.model.produce;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.teatrace.model.base.Device;
import com.teatrace.model.base.Employee;

@Entity
@Table(name="teatree_plant")
public class TeaTreePlant {

	@Id
	@Column(name="teatree_plant_id")
	@GeneratedValue(strategy=GenerationType.AUTO)
	private int teaTreePlantId;//对应数据库表中的teatree_plant_id
	
	@Column(name="plant_time",columnDefinition="date")
	private String plantTime;
	
	private String description;
	
	@OneToMany(cascade = { CascadeType.ALL},
			mappedBy ="teaTreePlant",
			fetch=FetchType.EAGER
	) 
	//这里配置关系，并且确定关系维护端和被维护端。
	//mappBy表示关系被维护端，只有关系端有权去更新外键。这里还有注意OneToMany默认的加载方式是赖加载。
	//当看到设置关系中最后一个单词是Many，那么该加载默认为懒加载   
	private Set<TeaTrees> teaTreeses = new HashSet<TeaTrees>();
	
	@ManyToMany
	@JoinTable(
		name="r_employee_teatreeplant",
		joinColumns=@JoinColumn(name="teatree_plant_id"),
		inverseJoinColumns=@JoinColumn(name="employee_id")
	)
	private Set<Employee> emps;//多对多
	
	@ManyToMany
	@JoinTable(
		name="r_device_teatreeplant",
		joinColumns=@JoinColumn(name="teatrees_plant_id"),
		inverseJoinColumns=@JoinColumn(name="device_id")
	)
	private Set<Device> devices;//多对多
	
	
	
	public int getTeaTreePlantId() {
		return teaTreePlantId;
	}
	public void setTeaTreePlantId(int teaTreePlantId) {
		this.teaTreePlantId = teaTreePlantId;
	}

	
	public String getPlantTime() {
		return plantTime;
	}
	public void setPlantTime(String plantTime) {
		this.plantTime = plantTime;
	}

	public Set<Employee> getEmps() {
		return emps;
	}
	public void setEmps(Set<Employee> emps) {
		this.emps = emps;
	}
	public Set<Device> getDevices() {
		return devices;
	}
	public void setDevices(Set<Device> devices) {
		this.devices = devices;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getDescription() {
		return description;
	}
	/**
	 * 在teaTreePlant 中加入teaTrees
	 * @param teaTrees
	 */
	public void addTeaTrees(TeaTrees teaTrees){
		if(!this.teaTreeses.contains(teaTrees)){
			teaTrees.setTeaTreePlant(this);
			this.teaTreeses.add(teaTrees);
		}
	}
	/**
	 * 在teaTreePlant中删除teaTrees
	 */
	public void removeTeaTrees(TeaTrees teaTrees){
		teaTrees.setTeaTreePlant(null);
		this.teaTreeses.remove(teaTrees);
	}
}
